package speechprocess;

/**
 *
 * @author Oanh
 */
public class Calculation {

    Magnitude[] m1;
    Magnitude[] m2;
    Magnitude[] m3;
    Complex[] hMul;
    double[] HMul;
    double x1Min;
    double x2Min;
    double x3Min;
    double H1Max;
    double H2Max;
    double H3Max;
    double xMax;
    double HMulMax;
    double[] x1;
    double[] x2;
    double[] x3;
    double[] atan1;
    double[] atan2;
    double[] atan3;
    double[] atan;

    public Calculation(double f1, double b1, double f2, double b2, double f3, double b3) {
        m1 = new Magnitude[5000];
        m2 = new Magnitude[5000];
        m3 = new Magnitude[5000];
        hMul = new Complex[5000];
        HMul = new double[5000];

        x1 = new double[5000];
        x2 = new double[5000];
        x3 = new double[5000];

        atan1 = new double[5000];
        atan2 = new double[5000];
        atan3 = new double[5000];
        atan = new double[5000];


        for (int f = 0; f < 5000; f++) {
            m1[f] = new Magnitude(f1, b1, f);
            m2[f] = new Magnitude(f2, b2, f);
            m3[f] = new Magnitude(f3, b3, f);
            hMul[f] = (m1[f].hF.mul(m2[f].hF)).mul(m3[f].hF);

            H1Max = max(H1Max, m1[f].hF.module());
            H2Max = max(H2Max, m2[f].hF.module());
            H3Max = max(H3Max, m3[f].hF.module());
            HMulMax = max(HMulMax, hMul[f].module());
        }

        for (int f = 0; f < 5000; f++) {
            x1[f] = 20 * Math.log10(m1[f].hF.module() / H1Max);
            x2[f] = 20 * Math.log10(m2[f].hF.module() / H2Max);
            x3[f] = 20 * Math.log10(m3[f].hF.module() / H3Max);
            HMul[f] = 20 * Math.log10(hMul[f].module() / HMulMax);

            x1Min = min(x1Min, x1[f]);
            x2Min = min(x2Min, x2[f]);
            x3Min = min(x3Min, x3[f]);            

            atan1[f] = Math.atan(-m1[f].hF.image / m1[f].hF.real);
            atan2[f] = Math.atan(-m2[f].hF.image / m2[f].hF.real);
            atan3[f] = Math.atan(-m3[f].hF.image / m3[f].hF.real);
            atan[f] = Math.atan(-hMul[f].image / hMul[f].real);
        }
        xMax = max(x1Min, x2Min, x3Min);
    }

    double max(double x, double y) {
        if (x > y) {
            return x;
        } else {
            return y;
        }
    }

    double min(double x, double y) {
        if (x < y) {
            return x;
        } else {
            return y;
        }
    }

    double max(double x, double y, double z) {
        double max;
        max = x;
        if (max < y) {
            max = y;
        }
        if (max < z) {
            max = z;
        }
        return max;
    }
    
    
}
